<?php

/**
 * This is the model class for table "sps_juso_users".
 *
 * The followings are the available columns in table 'sps_juso_users':
 * @property integer $id
 * @property string $username
 * @property string $salt
 * @property string $email
 * @property string $role
 *
 * The followings are the available model relations:
 * @property SpsWmProfilSql $benutzernummer
 */
class User extends CActiveRecord {

	// Variables used for search
	public $firstname;
	public $lastname;
	private $_salt = "demey#meh-ce";
	// Dummy field for password verification
	public $repeatPassword;

	/**
	 * Returns the static model of the specified AR class.
	 * @param string $className active record class name.
	 * @return User the static model class
	 */
	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return 'sps_juso_users';
	}

	/**
	 * @return array validation rules for model attributes.
	 */
	public function rules() {
		// NOTE: you should only define rules for those attributes that
		// will receive user inputs.
		return array(
			array('email, Benutzernummer', 'required', 'on' => 'update, insert'),
			array('password, repeatPassword', 'required', 'on' => 'insert'),
			array('Benutzernummer, password, repeatPassword, email', 'length', 'max' => 128),
			array('email', 'email'),
			array('Benutzernummer', 'exist', 'attributeName' => 'Kundennummer', 'className' => 'Membership'),
			array('role', 'length', 'max' => 64),
			array('password', 'compare', 'compareAttribute' => 'repeatPassword'),
			// The following rule is used by search().
			// Please remove those attributes that should not be searched.
			array('email, role, firstname, lastname', 'safe', 'on' => 'search'),
		);
	}

	/**
	 * @return array relational rules.
	 */
	public function relations() {
		// NOTE: you may need to adjust the relation name and the related
		// class name for the relations automatically generated below.
		return array(
			'profile' => array(self::BELONGS_TO, 'Member', array('Benutzernummer' => 'Benutzernummer'), 'joinType' => 'INNER JOIN'),
			'membership' => array(self::HAS_ONE, 'Membership', array('Benutzernummer_Web' => 'Benutzernummer'), 'joinType' => 'INNER JOIN', 'condition' => " Verband = 'JUSO Schweiz' AND (Austritt = '0000-00-00' OR Austritt >= '" . date('Y-m-d') . "')"),
		);
	}

	/**
	 * @return array customized attribute labels (name=>label)
	 */
	public function attributeLabels() {
		return array(
			'id' => 'ID',
			'Benutzernummer' => 'OM-Nummer',
			'password' => 'Passwort',
			'email' => 'E-Mail',
			'repeatPassword' => 'Passwort wiederholen',
			'role' => 'Rolle',
			'firstname' => 'Vorname',
			'lastname' => 'Name',
		);
	}

	/**
	 * Retrieves a list of models based on the current search/filter conditions.
	 * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
	 */
	public function search() {
		// Warning: Please modify the following code to remove attributes that
		// should not be searched.

		$criteria = new CDbCriteria;

		$criteria->with = array('profile' => array(
				'joinType' => 'INNER JOIN',
//				'condition' => $activeCondition . $regionConstraint,
//				'order' => 'Name ASC',
				));

		$criteria->compare('Benutzernummer', $this->Benutzernummer, true);
		$criteria->compare('email', $this->email, true);
		$criteria->compare('role', $this->role, true);
		$criteria->compare('profile.Vorname', $this->firstname, true);
		$criteria->compare('profile.Name', $this->lastname, true);

		return new CActiveDataProvider($this, array(
					'criteria' => $criteria,
				));
	}

	public function beforeSave() {
		$membership = Membership::model()->findByAttributes(array("Kundennummer" => $this->Benutzernummer));
		$this->Benutzernummer = $membership->Benutzernummer_Web;

		if (!empty($this->password)) {
			$this->password = $this->hashPassword($this->password);
		} else {
			unset($this->password);
		}

		return parent::beforeSave();
	}

	public function validatePassword($password) {
		return $this->hashPassword($password) === $this->password;
	}

	public function hashPassword($password) {
		return md5($this->_salt . $password);
	}

}